package com.yixun.qfbao.api.service.impl;

import com.yixun.qfbao.api.service.BaseCodeTypeService;
import com.yixun.qfbao.api.service.BaseCodeValueService;
import com.yixun.qfbao.apidto.*;
import com.yixun.qfbao.mapper.BaseCodeTypeMapper;
import com.yixun.qfbao.model.BaseCodeType;
import com.yixun.qfbao.model.ZNodes;
import com.yixun.qfbao.model.dto.BaseCodeTypeDto;
import com.yixun.qfbao.model.dto.BaseCodeValueDto;
import com.yixun.qfbao.model.enums.BaseCodeTypeEnum;
import com.yixun.qfbao.model.enums.DelFlagEnum;
import com.yixun.qfbao.xf.model.enums.ResultEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 字典类型业务
 *
 * @author guifang.weng
 * @create 2017-11-24 11:47
 **/
@Service
public class BaseCodeTypeServiceImpl implements BaseCodeTypeService {

    @Autowired
    private BaseCodeTypeMapper baseCodeTypeMapper;

    @Autowired
    private BaseCodeValueService baseCodeValueService;

    @Override
    public GetBaseCodeTypeResDto getBaseCodeType(GetBaseCodeTypeReqDto getBaseCodeTypeReqDto) throws Exception {
        GetBaseCodeTypeResDto resDto = new GetBaseCodeTypeResDto();
        BaseCodeTypeDto dto = new BaseCodeTypeDto();
        dto.setCodeType(getBaseCodeTypeReqDto.getCodeType());
        dto.setMerchantId(getBaseCodeTypeReqDto.getMerchantId());
        List<BaseCodeType> basecodeTypeList = baseCodeTypeMapper.selectListByDto(dto);
        resDto.setResult(ResultEnum.SUCCESS.getNo());
        resDto.setBaseCodeTypeList(basecodeTypeList);
        return resDto;
    }

    @Override
    public GetBaseCodeTypeTreeResDto getBaseCodeTypeTree(GetBaseCodeTypeTreeReqDto getBaseCodeTypeTreeReqDto) throws Exception {
        GetBaseCodeTypeTreeResDto resDto = new GetBaseCodeTypeTreeResDto();
        BaseCodeTypeDto baseCodeTypeDto = new BaseCodeTypeDto();
        baseCodeTypeDto.setMerchantId(getBaseCodeTypeTreeReqDto.getMerchantId());
        List<BaseCodeType> deptList = baseCodeTypeMapper.selectListByDto(baseCodeTypeDto);
        //组装Znodes
        List<ZNodes> nodesList = new ArrayList<>();
        for (BaseCodeType dt : deptList) {
            ZNodes zNode = new ZNodes();
            zNode.setId(String.valueOf(dt.getId()));
            zNode.setpId(String.valueOf(dt.getParentId()));
            zNode.setName(dt.getTypeName());
            zNode.setOther(dt.getRemark());
            nodesList.add(zNode);
        }
        resDto.setResult(ResultEnum.SUCCESS.getNo());
        resDto.setNodesList(nodesList);
        return resDto;
    }

    @Override
    public AddBaseCodeTypeResDto addBaseCodeType(AddBaseCodeTypeReqDto addBaseCodeTypeReqDto) throws Exception {
        AddBaseCodeTypeResDto resDto = new AddBaseCodeTypeResDto();
        BaseCodeType entity = new BaseCodeType();
        entity.setTypeName(addBaseCodeTypeReqDto.getTypeName());
        entity.setRemark(addBaseCodeTypeReqDto.getRemark());
        entity.setMerchantId(addBaseCodeTypeReqDto.getMerchantId());
        entity.setCodeType(BaseCodeTypeEnum.TYPE_ONE.getNo());
        entity.setCreateUserId(Long.valueOf(addBaseCodeTypeReqDto.getUserId()));
        entity.setCreateTime(new Date());
        entity.setDelFlag(DelFlagEnum.UNDEL.getNo());
        if (addBaseCodeTypeReqDto.getParentId() == null) {
            entity.setParentId(0L);
        } else {
            entity.setParentId(Long.valueOf(addBaseCodeTypeReqDto.getParentId()));
        }
        if (baseCodeTypeMapper.add(entity) <= 0) {
            resDto.setResult(ResultEnum.FAIL.getNo());
            resDto.setResultMsg("新增标签失败");
        } else {
            resDto.setResult(ResultEnum.SUCCESS.getNo());
            resDto.setResultMsg("新增标签成功");
        }
        return resDto;
    }

    @Override
    public EditBaseCodeTypeResDto editBaseCodeType(EditBaseCodeTypeReqDto editBaseCodeTypeReqDto) throws Exception {
        EditBaseCodeTypeResDto resDto = new EditBaseCodeTypeResDto();
        BaseCodeType baseCodeType = new BaseCodeType();
        baseCodeType.setId(editBaseCodeTypeReqDto.getId());
        baseCodeType.setRemark(editBaseCodeTypeReqDto.getRemark());
        baseCodeType.setParentId(editBaseCodeTypeReqDto.getParentId());
        baseCodeType.setTypeName(editBaseCodeTypeReqDto.getTypeName());
        baseCodeType.setUpdateUserId(Long.valueOf(editBaseCodeTypeReqDto.getUserId()));
        baseCodeType.setUpdateTime(new Date());
        if (baseCodeTypeMapper.edit(baseCodeType) <= 0) {
            resDto.setResult(ResultEnum.FAIL.getNo());
            resDto.setResultMsg("编辑标签失败");
        } else {
            resDto.setResult(ResultEnum.SUCCESS.getNo());
            resDto.setResultMsg("编辑标签成功");
        }
        return resDto;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public DelBaseCodeTypeResDto delBaseCodeType(DelBaseCodeTypeReqDto delBaseCodeTypeReqDto) throws Exception {
        DelBaseCodeTypeResDto resDto = new DelBaseCodeTypeResDto();
        boolean hasPartnerFlag = hasPartner(delBaseCodeTypeReqDto.getId());
        if (hasPartnerFlag) {
            resDto.setResult(ResultEnum.FAIL.getNo());
            resDto.setResultMsg("标签组或子标签组下存有值,不能删除!");
        } else {
            BaseCodeTypeDto baseCodeType = new BaseCodeTypeDto();
            baseCodeType.setId(delBaseCodeTypeReqDto.getId());
            baseCodeTypeMapper.del(baseCodeType);
            //删除子标签
            delDept(delBaseCodeTypeReqDto.getId());
            resDto.setResult(ResultEnum.SUCCESS.getNo());
            resDto.setResultMsg("删除标签成功");
        }
        return resDto;
    }

    public void delDept(long id) {
        BaseCodeTypeDto childDeptDto = new BaseCodeTypeDto();
        childDeptDto.setParentId(id);
        List<BaseCodeType> deptList = baseCodeTypeMapper.selectListByDto(childDeptDto);
        if (null != deptList && deptList.size() > 0) {
            baseCodeTypeMapper.del(childDeptDto);
            for (BaseCodeType dept : deptList) {
                delDept(dept.getId());
            }
        }
    }

    /**
     * 删除部门前，查询部门中是否存在员工
     *
     * @param id
     * @return
     */
    public boolean hasPartner(long id) {
        boolean hasPartnerFlag = false;
        BaseCodeValueDto bcvDto = new BaseCodeValueDto();
        bcvDto.setTypeId(id);
        bcvDto.setDelFlag(DelFlagEnum.UNDEL.getNo());
        int piCount = baseCodeValueService.count(bcvDto);
        if (piCount > 0) {
            hasPartnerFlag = true;
        } else {
            BaseCodeTypeDto bctDto = new BaseCodeTypeDto();
            bctDto.setParentId(id);
            List<BaseCodeType> deptList = baseCodeTypeMapper.selectListByDto(bctDto);
            if (null != deptList && deptList.size() > 0) {
                for (BaseCodeType dept : deptList) {
                    if (hasPartnerFlag) {
                        break;
                    }
                    hasPartner(dept.getId());
                }
            }
        }
        return hasPartnerFlag;
    }
}
